1,创建maven 项目 ,pom.xml中 配置springcloud和springboot的版本,和jdk和字符集版本大致思路:指定springcloud和spingboot版本,先搭建注册中心,再次搭建提供者和消费者的服务,注册到注册中心,配置fegin 再配置降级
1.1配置 Eureka注册中心 配置 application.yml 文件 这里做两个注册中心集群UTF-8 UTF-8 1.8 Finchley.SR1 2.0.5.RELEASE org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies${springboot.version} pom import
server: port: 1020 eureka: instance: hostname: eureka1 #显示的注册中心的实列名字 不能一样 client: registerWithEureka: false #是否要注册到eureka fetchRegistry: false #表示是否从Eureka Server获取注册信息 serviceUrl: defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka spring: application: name: eureka1 #给注册中心取个名字 名字不能一样 profiles: e1 #用于run启动可以根据 此名字加载对应的配置 --- server: port: 1030 eureka: instance: hostname: eureka1 #显示的注册中心的实列名字 不能一样 client: registerWithEureka: false #是否要注册到eureka fetchRegistry: false #表示是否从Eureka Server获取注册信息 serviceUrl: defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka spring: application: name: eureka2 #给注册中心取个名字 名字不能一样 profiles: e2 #用于run启动可以根据 此名字加载对应的配置
配置电脑的 host文件 路径 C:WindowsSystem32driversetc 加以下内容 ,启动的时候会自动以eureka1找到代理的地址127.0.0.1 IP
配置 启动类--本机地址 映射域名
127.0.0.1 eureka1
127.0.0.1 eureka2
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer//启用eureka注册服务 public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class); } }2搭建 服务提供者。 导入依赖
配置提供者启动类org.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntime org.mybatis.spring.boot mybatis-spring-boot-starter1.1.1 junit junit4.12 org.springframework.boot spring-boot-starter-testorg.springframework.cloud spring-cloud-starter-netflix-hystrix
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient//启用eureka客户端 @MapperScan("cn.*.mapper")//扫描mapper public class ServerApp { public static void main(String[] args) { SpringApplication.run(ServerApp.class); } }配置提供者yml文件
server: port: 8084 spring: datasource: url: jdbc:mysql://localhost:3306/itsource-course?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver mybatis: # mapper-locations: classpath*:cn/itsouce/mapper/*Mapper.xml type-aliases-package: cn.itsource.domain application: name: course-server #不要有下划线 profiles: s1 eureka: client: service-url: defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka instance: prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称 ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找 instance-id: course-server1 --- server: port: 8083 spring: application: name: course-server #不要有下划线 集群的名字大家都要一样,因为调用的时候要通过名字找 profiles: s2 eureka: client: service-url: defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka instance: prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称 ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找 instance-id: course-server2 logging: #日志打印 level: root: info cn: wl: debug3搭建服务消费者
@FeignClient(value = "course-server" ,fallbackFactory = MyBack.class) @RequestMapping("/course") public interface IFeginServer { ///此处代码从提供者复制 //MyBack为此接口的实现类 }
server: port: 7071 spring: datasource: url: jdbc:mysql://localhost:3306/itsource-buy?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver mybatis: # mapper-locations: classpath*:cn/itsouce/mapper/*Mapper.xml type-aliases-package: cn.itsource.domain #配置别名 application: name: buy-server #不要有下划线 eureka: client: service-url: defaultZone: http://eureka1:1020/eureka,http://eureka2:1030/eureka instance: prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称 ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找 instance-id: buy-server1 logging: #日志打印 level: root: info cn: wl: debug feign: hystrix: enabled: true #开启熔断支持 client: config: remote-service: #服务名,为course-server熔断 connectTimeout: 3000 readTimeout: 3000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000创建 服务消费者 启动类
package cn.itsouce; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient//启用eureka客户端 @MapperScan("cn.*.mapper")//扫描mapper @EnableFeignClients//启用feign调用 public class BuyApp { public static void main(String[] args) { SpringApplication.run(BuyApp.class); } @Bean @LoadBalanced public RestTemplate getRestTemplat(){ System.out.println("在启动类加上@Bean注解,spring" + "容器就加载此对象"); return new RestTemplate(); } @Bean//注入随机调用 public IRule getRandom(){ System.out.println("返回值为IRule,运行的确是RandomRule对象。这里用多态。实现随机调用"); return new RandomRule(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)