? DeptService(接口)
package com.gk.service;
import com.gk.bean.Dept;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient(value = "MYSC-DEPT")
@Component
public interface DeptService {
@RequestMapping("byId/{id}")
public Dept salById(@PathVariable(value = "id") Long id);
@RequestMapping("salList")
public List<Dept> salByList();
@RequestMapping("salByInfo")
public Object getDiscovery();
}
@SpringBootApplication
@EnableEurekaServer
public class EurekaStart {
public static void main( String[] args ){
SpringApplication.run(EurekaStart.class,args);
}
}
server:
port: 7001
# eureka相关
eureka:
instance:
hostname: activate.navicat.com
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #fetch-registry为false,表示注册中心是自己
service-url:
defaultZone: http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动springBoot类、pom在eureka7001模块基础上 都不变
server:
port: 7002
# eureka相关
eureka:
instance:
hostname: activate.navicat2.com
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #fetch-registry为false,表示注册中心是自己
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat3.com:7003/eureka/
<dependencies>
<dependency>
<groupId>com.gk</groupId>
<artifactId>mySc-bean</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
server:
port: 8000
spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root
#eureka相关
eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-8000
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud
company.name: 这是注定,这是命运
@Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {
}
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gk.bean.Dept;
import com.gk.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DeptController {
@Autowired
private DeptMapper deptMapper;
@Autowired
private DiscoveryClient client;//可以用来获取服务信息
@RequestMapping("byId/{id}")
public Dept salById(@PathVariable Long id){
QueryWrapper<Dept> dw = new QueryWrapper<>();
dw.eq("deptno",id);
Dept dept = deptMapper.selectOne(dw);
return dept;
}
@RequestMapping("salList")
public List<Dept> salByList(){
List<Dept> depts = deptMapper.selectList(null);
return depts;
}
@RequestMapping("salByInfo")
public Object getDiscovery(){
//获取微服务列表清单
List<String> list = client.getServices();
System.out.println(list);
System.out.println("================");
//通过微服务的Application(注册在eureka中的实例) 获取某个具体的微服务信息
List<ServiceInstance> instances = client.getInstances("MYSC-DEPT");//获取mySc-dept模块的服务信息
for (ServiceInstance s : instances) {
System.out.println(
s.getHost() + "\n"+
s.getInstanceId() + "\n"+
s.getScheme() + "\n"+
s.getServiceId() + "\n"+
s.getMetadata() + "\n"+
s.getPort() + "\n"+
s.getUri()+"\n"+
"==============="
);
}
return instances;
}
}
@Repository
public interface DeptMapper extends BaseMapper<Dept> {
}
@SpringBootApplication
@EnableEurekaClient
public class MyScDeptApplication {
public static void main(String[] args) {
SpringApplication.run(MyScDeptApplication.class,args);
}
}
在第一个提供者之上没太对变化
server:
port: 8002
spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root
#eureka相关
eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-8002
ip-address: true # 显示服务器的ip地址
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud2
company.name: 这是注定,这是命运2
server:
port: 8003
spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root
#eureka相关
eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-dept3
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud3
company.name: 这是注定,这是命运3
<dependencies>
<!--ribbon负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.gk</groupId>
<artifactId>mySc-bean</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
server:
port: 8001
eureka:
client:
register-with-eureka: false #不让在eureka注册中心注册自己
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
@Configuration
public class DeptConfig {
//配置负载均衡实现RestTemplate
@Bean
// IRule: 负载均衡策略(接口)以下为他的实现类
//RoundRobinRule:轮循(默认)
//RandomRule: 随机访问
//AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
//RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
@LoadBalanced //使用ribbon实现负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
public class DeptController {
//请求前缀地址
// private final String PREFIX_URL="http://localhost:8000/";
private final String PREFIX_URL="http://MYSC-DEPT/";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("salById/{id}")
public Dept salById(@PathVariable Long id){
return restTemplate.getForObject(PREFIX_URL+"byId/"+id,Dept.class);
}
@RequestMapping("salByList")
public List<Dept> salByList(){
return restTemplate.getForObject(PREFIX_URL + "salList",List.class);
}
}
@SpringBootApplication
@EnableEurekaClient
public class MyScConsumer {
public static void main(String[] args) {
SpringApplication.run(MyScConsumer.class,args);
}
}
<dependencies>
<dependency>
<groupId>com.gk</groupId>
<artifactId>mySc-bean</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
</dependencies>
server:
port: 8005
spring:
application:
name: mySc-dept-feign
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root
#eureka相关
eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-feign-8005
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud2
company.name: 这是注定,这是命运2
@Configuration
@ComponentScan("com.gk")
public class DeptConfig {
//配置负载均衡实现RestTemplate
@Bean
// IRule: 负载均衡策略(接口)一下为他的实现类
//RoundRobinRule:轮循(默认)
//RandomRule: 随机访问
//AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
//RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
@LoadBalanced //使用ribbon实现
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("salById/{id}")
public Dept salById(@PathVariable(value="id") Long id){
return deptService.salById(id);
}
@RequestMapping("salByList")
public List<Dept> salByList(){
return deptService.salByList();
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"com.gk"})
public class FeignScConsumer {
public static void main(String[] args) {
SpringApplication.run(FeignScConsumer.class,args);
}
}
<dependencies>
<dependency>
<groupId>com.gk</groupId>
<artifactId>mySc-bean</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
</dependencies>
@Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {
}
@RestController
public class DeptController {
@Autowired
private DeptMapper deptMapper;
@RequestMapping("byId/{id}")
@HystrixCommand(fallbackMethod="salById2")//方法异常后调用salById2方法
public Dept salById(@PathVariable Long id){
QueryWrapper<Dept> dw = new QueryWrapper<>();
dw.eq("deptno",id);
Dept dept = deptMapper.selectOne(dw);
if (dept==null){
throw new RuntimeException("查询结果为空...");
}
return dept;
}
public Dept salById2(@PathVariable Long id){
return new Dept(id,"没有当前这个id=>"+id,"没有这个数据库");
}
}
@Repository
public interface DeptMapper extends BaseMapper<Dept> {
}
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker //添加熔断支持
public class MyHystrixScDeptApplication {
public static void main(String[] args) {
SpringApplication.run(MyHystrixScDeptApplication.class,args);
}
}
server:
port: 8004
spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root
#eureka相关
eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-hystrix-8004
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud3
company.name: 这是注定,这是命运3
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
server:
port: 9527
# spring 配置服务名字
spring:
application:
name: mySc-zuul-9527
#eureka
eureka:
instance:
instance-id: mySc-zuul-9527
prefer-ip-address: true # 显示id地址
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
info:
app.name: zhangsan.com
app.address: www.疯子.com
zuul:
routes:
dept.serviceId: mysc-dept
dept.path: /dept/**
ignored-services: "*" # 不允许任何一个路径进行访问,只允许使用 dept(http://www.gk.com:9527/dept/) 进行访问
prefix: /gk #设置公共的访问前缀 :http://www.gk.com:9527/gk/dept/
@SpringBootApplication
@EnableZuulProxy
public class MyZuulApplication {
public static void main( String[] args ){
SpringApplication.run(MyZuulApplication.class,args);
}
}
原文:https://www.cnblogs.com/gekun154/p/15150575.html