1.服务端
1.1.父工程pom.xml需指定SpringCloud版本
<dependencyManagement> // 锁定版本
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2.子工程引入eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.3.yml 配置
server:
port: 6868 #服务端口
eureka:
client:
registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所以无需注册
fetchRegistry: false #是否从Eureka中获取注册信息
serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka/
1.4.编写服务端启动类
启动类添加注解:@EnableEurekaServer
package com.tensquare.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class);
}
}
1.5.保护模式
启动后访问 http://localhost:6868/
2.客户端
2.1.服务注册
2.1.1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.1.2.yml配置-注意端口监听的是服务端配置好的端口
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:**6868**/eureka/
instance:
prefer-ip-address: true
2.1.3.编写客户端启动类
启动类添加注解:@EnableEurekaClient
package com.tensquare.article;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
@SpringBootApplication
@EnableEurekaClient
public class ArticleApplication {
public static void main(String[] args) {
SpringApplication.run(ArticleApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
}
多个客户端如上操作即可,然后在docker中启动所需服务。
3.eureka服务之间的调用–feign
3.1.在调用方引入feign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐openfeign</artifactId>
</dependency>
3.2.编写调用接口与实现类
接口:
package com.tensquare.qa.client;
import com.tensquare.qa.client.impl.BaseClientImpl;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
@FeignClient注解用于指定从哪个服务中调用功能 ,注意 里面的名称与被调用的服务
名保持一致,并且不能包含下划线。
* value:模块名
* fallback:出异常后,熔断器跳转到实现处理类
*/
@FeignClient(value = "tensquare-base", fallback = BaseClientImpl.class)
public interface BaseClient {
// 这里指定了被调用方的接口路径
@RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
public Result findById(@PathVariable("labelId") String labelId);
}
实现类:
package com.tensquare.qa.client.impl;
import com.tensquare.qa.client.BaseClient;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Component;
@Component
public class BaseClientImpl implements BaseClient {
@Override
public Result findById(String labelId) {
return new Result(false, StatusCode.ERROR, "熔断器触发了!");
}
}
3.3.配置调用方的启动类
启动类添加注解:
@EnableDiscoveryClient
@EnableFeignClients
package com.tensquare.qa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
import util.JwtUtil;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class QaApplication {
public static void main(String[] args) {
SpringApplication.run(QaApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
@Bean
public JwtUtil jwtUtil(){
return new JwtUtil();
}
}