?
?
?2.1.3 增加依賴
<!--添加依賴-->
<dependencies>
<!--Eureka Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--增加公共方法-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監(jiān)控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
?2.1.4?yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-register: false
server-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
?2.1.5編寫主啟動類
@EnableEurekaServer
@SpringBootApplication
public class EurakeServer7001Application {
public static void main(String[] args) {
SpringApplication.run(EurakeSever7001Application.class,args);
}
}
2.2.2修改pom添加依賴
<!--添加Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2.3修改yml:添加Eureka的配置?
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true 單節(jié)點無所謂,集群必須設置true 才能配合ribbon 使用負載均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注冊
2.2.4修改主啟動類:標注Eureka客戶端
@SpringBootApplication
@EnableEurekaClient
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
?2.3.2修改pom添加依賴
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
?2.3.3 修改yml:添加Eureka的配置
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true 單節(jié)點無所謂,集群必須設置true 才能配合ribbon 使用負載均衡
fetch-registry: true
server-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注冊
2.3.4修改啟動類 標注Eureka客戶端
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
?
?2.3.4修改主啟動類 標注為Eureka客戶端
2.3.6補充
// private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
//根據id查詢
@GetMapping("/consumer/payment/get/{id}")
public ResponseResult queryById(@PathVariable("id") Integer id){
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.getForObject("http://"+instance.getHost()+instance.getPort()+"/payment/get/"+id,ResponseResult.class);
// ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);
return rs;
}
//創(chuàng)建訂單
@GetMapping("/consumer/payment/save")
public ResponseResult save(Payment payment){
// ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.postForObject("http://"+instance.getHost()+instance.getPort()+"/payment/save",payment,ResponseResult.class);
return rs;
}
}
?
?3.2.1修改pom添加依賴
<!--依賴-->
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--公共方法-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--熱部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.2.2編寫yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-registry: false
service-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
?3.2.5 編寫啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7002Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002Application.class,args);
}
}
?
?
?
?3.3.3修改pom添加依賴
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--公共類-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
?3.3.4編寫yml
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-registry: false
service-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
?3.3.5編寫啟動類
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7003Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7003Application.class,args);
}
}
?
3.4.2 修改后的配置文件?
springcloud-eureka-sever-7001
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-register: false
service-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7002/eureka/,http://eureka7003.com:7002/eureka/
springcloud-eureka-sever-7001?
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-registry: false
service-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
?springcloud-eureka-sever003
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服務器端的
client:
#false 表示不向注冊中心注冊自己
register-with-eureka: false
#false 表示自己端就是注冊中心,我的職責就是維護服務實例,并不需要去檢索服務
fetch-registry: false
service-url:
#設置與Eureka server交互的地址查詢服務和注冊服務都需要依賴這個地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
?
?
4.1支付微服務發(fā)布到?Eureka Sever中
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity別名類所在包
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true 單節(jié)點無所謂,集群必須設置true 才能配合ribbon 使用負載均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注冊
?
?
?
?4.2訂單微服務發(fā)布到Eureka Server中?
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity別名類所在包
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true 單節(jié)點無所謂,集群必須設置true 才能配合ribbon 使用負載均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注冊
?
5.2.2修改pom 添加依賴
<!--依懶-->
<dependencies>
<!--添加Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--公共的部分-->
<dependency>
<groupId>cn.bdqn</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監(jiān)控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--數據庫-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--阿里云數據源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
5.2.3編輯yml?
server:
port: 8002
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain
eureka:
client:
#表示是否將自己注冊進EurekaServer默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的注冊信息,默認為true 單節(jié)點無所謂,集群必須設置true 才能配合ribbon 使用負載均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注冊
5.24編寫啟動類
@SpringBootApplication
@EnableEurekaClient
public class Payment8002Application {
public static void main(String[] args) {
SpringApplication.run(Payment8002Application.class,args);
}
}
5.25編寫PaymentMapper接口?
@Mapper
public interface PaymentMapper {
//保存一個支付流水
public void insert(Payment payment);
//根據id獲取具體的支付信息
public Payment selectById(@Param("id") Integer id);
}
5.2.6編寫PaymentMapper.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "--//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bdqn.Mapper.PaymentMapper">
<resultMap id="PaymentResultMap" type="cn.bdqn.domain.Payment">
<id column="id" property="id"></id>
<id column="flow_number" property="flowNumber"></id>
</resultMap>
<insert id="insert" parameterType="cn.bdqn.Mapper.PaymentMapper">
insert into t_payment(flow_number) values(#{flowNumber})
</insert>
<select id="selectById" resultMap="PaymentResultMap">
select id,flow_number from t_payment where id=#{id};
</select>
</mapper>
?5.
5.2.7編寫payment業(yè)務接口以及實現類
public interface PaymentServer {
//保存一個支付流水
public void save(Payment payment);
//根據id獲取具體的支付信息
public Payment queryById(Integer id);
}
@Service
public class PaymentServerImpl implements PaymentServer {
@Autowired
private PaymentMapper paymentMapper;
@Override
public Payment queryById(Integer id) {
return paymentMapper.selectById(id);
}
@Override
public void save(Payment payment) {
paymentMapper.insert(payment);
}
}
?
5.2.8? 編寫paymentController控制器
@RestController
public class PaymentConroller {
@Autowired
private PaymentServerImpl paymentServer;
@GetMapping("/payment/get/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment = paymentServer.queryById(id);
if(payment!=null) {
return new ResponseResult(200,"成功",payment);
}else{
return new ResponseResult(404,"沒有對應的記錄,查詢id"+id,null);
}
}
@PostMapping("/payment/save")
public ResponseResult save(@RequestBody Payment payment){
try {
paymentServer.save(payment);
return new ResponseResult(200,"插入成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(200,"插入失敗",null);
}
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-606363.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-606363.html
到了這里,關于四,Eureka 第四章的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!