SpringCloud 整合OpenFeign 實作聲明式服務調用
- SpringCloud 整合OpenFeign
-
-
-
- 至此服務間調用openFeign 整合完成代碼在哪裡??[我在這!!!!!](https://gitee.com/chen123b_admin/spring-cloud-scaffolding)
-
-
本篇文章繼續在: SpringCloud(Springboot)+Mybatis Plus . 基礎上完善
SpringCloud 整合OpenFeign
- nacos-config 參照:SpringCloud(Springboot)+Mybatis Plus. 實作ConfigController
package com.chen.scaffolding.nacosconfig.controller;
import com.chen.scaffolding.nacosconfig.entity.Authority;
import com.chen.scaffolding.nacosconfig.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 測試nacos-config
* @author :master
*/
@RestController
@RefreshScope
@RequestMapping("/config")
public class ConfigController {
@Autowired
private AuthorityService authorityService;
@Value("${useLocalCache}")
private String useLocalCache;
/**
* http://localhost:8080/config/get
*/
@RequestMapping("/get")
public String get() {
return useLocalCache;
}
@RequestMapping("addAuthority")
public String addAuthority (@RequestBody Authority authority) {
authorityService.addAuthority(authority);
return "權限設定成功!";
}
@RequestMapping("upateAuthority")
public String upateAuthority (@RequestBody Authority authority) {
try {
authorityService.updateAuthority(authority);
return "權限修改成功!";
}catch (Exception e) {
return e.getMessage();
}
}
}
- 分别在項目 nacos-server裡面引入OpenFeign 包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
- 在service 包裡建立OpenFeignAuthorityService
package com.chen.scaffolding.nacosserver.service.impl;
import com.chen.scaffolding.nacosconfig.entity.Authority;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 調用權限服務
* @author :master
*/
@Service
// value 指明調用服務名稱, url服務位址
@FeignClient(value = "nacos-config", url = "http://localhost:8002/config")
public interface OpenFeignAuthorityService {
@RequestMapping("addAuthority")
public String addAuthority (@RequestBody Authority authority);
@RequestMapping("upateAuthority")
public String upateAuthority (@RequestBody Authority authority);
}
- 在nacos-server實作類裡面調用nacos-config
package com.chen.scaffolding.nacosserver.service.impl;
import com.chen.scaffolding.nacosconfig.entity.Authority;
import com.chen.scaffolding.nacosserver.entity.UserEntity;
import com.chen.scaffolding.nacosserver.mapper.UserMapper;
import com.chen.scaffolding.nacosserver.service.UserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 使用者類
* @author :master
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private OpenFeignAuthorityService authorityService;
@Autowired
private UserMapper userMapper;
@Override
@GlobalTransactional(rollbackFor = Exception.class)
@Transactional(rollbackFor = Exception.class)
public void addUser(UserEntity userEntity) {
// 第一步
userMapper.addUser(userEntity);
// 第二步
Authority authority = new Authority();
authority.setAuthId("1");
authority.setAuthName("增權重限");
authority.setUserId("1");
authorityService.addAuthority(authority);
}
@Override
public void updateUser(UserEntity userEntity) {
userMapper.updateUser(userEntity);
}
}
- 啟動類上增加@EnableFeignClients
- 啟動項目通路http://localhost:8086/server/addUser, 檢查資料庫 nacos-server資料庫: nacos-config資料庫: